package com.webserver.core;

import annotations.Controller;
import annotations.RequestMapping;
import com.webserver.controller.ArticleController;
import com.webserver.controller.ToolsController;
import com.webserver.controller.UserController;
import com.webserver.http.HttpContext;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用来完成处理请求的环节
 */
public class DispatcherServlet {
    public void service(HttpServletRequest request, HttpServletResponse response) throws URISyntaxException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
//        String path = request.getUri();
        String path = request.getRequestURI();
        System.out.println("请求的抽象路径:" + path);
        //首先判断是否为请求一个业务处理
        /*
            1:扫描com/webserver/controller目录,并将所有的.class文件获取回来
            并根据文件名截取类名利用反射加载回来

            2:判断该类是否被@Controller标注,是的话则获取该类中定义的所有方法

            3:每个方法先判断是否被@RequestMapping标注过,若被标注,则获取这个注解
              并得到value的值(@RquestMapping("这个值"))
            4:根据value值判断是否为本次请求路径(与path进行equals比较).如果与本次请求
              路径一致则说明就是这个方法处理该请求,然后通过调用invoke执行该方法
              注意:执行后调用return结束.避免后续的分支继续执行

         */

        File dir = new File(
               DispatcherServlet.class.getClassLoader().getResource("./com/webserver/controller").toURI()
        );
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File file : subs) {
            String fileName = file.getName();
            String className = fileName.substring(0, fileName.indexOf("."));
            Class cls = Class.forName("com.webserver.controller."+className);
            if (cls.isAnnotationPresent(Controller.class)) {
                System.out.println(className + ":被@AutoRunClass标注!");
                Object o = cls.newInstance();
                Method[] methods = cls.getDeclaredMethods();
                for (Method method : methods) {
                    if(method.isAnnotationPresent(RequestMapping.class)){
                        System.out.println(method.getName()+":被@AutoRunClass标注!");
                        RequestMapping arm =method.getAnnotation(RequestMapping.class);
                        String value =arm.value();
                        System.out.println("自动执行"+method.getName()+"方法"+value+"次:");
                        if(value.equals(path)){
                            method.invoke(o,request,response);
                            return;
                        }
                    }
                }
            }
        }


        File file = new File("./webapps" + path);
        if (file.isFile()) {
            response.setContentFile(file);
        } else {
            response.setStatusCode(404);
            response.setStatusReason("No Found");
            file = new File("./webapps/root/404.html");
            response.setContentFile(file);
        }
        response.addHeader("Server", "WebServer");

    }
}
